﻿using System;
using UnityEngine;

namespace RenderHeads.Media.AVProVideo
{
	// Token: 0x02000018 RID: 24
	[AddComponentMenu("AVPro Video/Apply To Mesh", 300)]
	[HelpURL("http://renderheads.com/product/avpro-video/")]
	public class ApplyToMesh : MonoBehaviour
	{
		// Token: 0x17000009 RID: 9
		// (get) Token: 0x06000083 RID: 131 RVA: 0x00005E18 File Offset: 0x00004018
		// (set) Token: 0x06000084 RID: 132 RVA: 0x00005E20 File Offset: 0x00004020
		public MediaPlayer Player
		{
			get
			{
				return this._media;
			}
			set
			{
				if (this._media != value)
				{
					this._media = value;
					this._isDirty = true;
				}
			}
		}

		// Token: 0x1700000A RID: 10
		// (get) Token: 0x06000085 RID: 133 RVA: 0x00005E44 File Offset: 0x00004044
		// (set) Token: 0x06000086 RID: 134 RVA: 0x00005E4C File Offset: 0x0000404C
		public Texture2D DefaultTexture
		{
			get
			{
				return this._defaultTexture;
			}
			set
			{
				if (this._defaultTexture != value)
				{
					this._defaultTexture = value;
					this._isDirty = true;
				}
			}
		}

		// Token: 0x1700000B RID: 11
		// (get) Token: 0x06000087 RID: 135 RVA: 0x00005E70 File Offset: 0x00004070
		// (set) Token: 0x06000088 RID: 136 RVA: 0x00005E78 File Offset: 0x00004078
		public Renderer MeshRenderer
		{
			get
			{
				return this._mesh;
			}
			set
			{
				if (this._mesh != value)
				{
					this._mesh = value;
					this._isDirty = true;
				}
			}
		}

		// Token: 0x1700000C RID: 12
		// (get) Token: 0x06000089 RID: 137 RVA: 0x00005E9C File Offset: 0x0000409C
		// (set) Token: 0x0600008A RID: 138 RVA: 0x00005EA4 File Offset: 0x000040A4
		public string TexturePropertyName
		{
			get
			{
				return this._texturePropertyName;
			}
			set
			{
				if (this._texturePropertyName != value)
				{
					this._texturePropertyName = value;
					this._isDirty = true;
				}
			}
		}

		// Token: 0x1700000D RID: 13
		// (get) Token: 0x0600008B RID: 139 RVA: 0x00005EC8 File Offset: 0x000040C8
		// (set) Token: 0x0600008C RID: 140 RVA: 0x00005ED0 File Offset: 0x000040D0
		public Vector2 Offset
		{
			get
			{
				return this._offset;
			}
			set
			{
				if (this._offset != value)
				{
					this._offset = value;
					this._isDirty = true;
				}
			}
		}

		// Token: 0x1700000E RID: 14
		// (get) Token: 0x0600008D RID: 141 RVA: 0x00005EF4 File Offset: 0x000040F4
		// (set) Token: 0x0600008E RID: 142 RVA: 0x00005EFC File Offset: 0x000040FC
		public Vector2 Scale
		{
			get
			{
				return this._scale;
			}
			set
			{
				if (this._scale != value)
				{
					this._scale = value;
					this._isDirty = true;
				}
			}
		}

		// Token: 0x0600008F RID: 143 RVA: 0x00005F20 File Offset: 0x00004120
		public void ForceUpdate()
		{
			this._isDirty = true;
			this.LateUpdate();
		}

		// Token: 0x06000090 RID: 144 RVA: 0x00005F30 File Offset: 0x00004130
		private void Awake()
		{
			if (ApplyToMesh._propStereo == 0 || ApplyToMesh._propAlphaPack == 0)
			{
				ApplyToMesh._propStereo = Shader.PropertyToID("Stereo");
				ApplyToMesh._propAlphaPack = Shader.PropertyToID("AlphaPack");
				ApplyToMesh._propApplyGamma = Shader.PropertyToID("_ApplyGamma");
			}
			if (ApplyToMesh._propChromaTex == 0)
			{
				ApplyToMesh._propChromaTex = Shader.PropertyToID("_ChromaTex");
			}
			if (ApplyToMesh._propUseYpCbCr == 0)
			{
				ApplyToMesh._propUseYpCbCr = Shader.PropertyToID("_UseYpCbCr");
			}
		}

		// Token: 0x06000091 RID: 145 RVA: 0x00005FB0 File Offset: 0x000041B0
		private void LateUpdate()
		{
			bool flag = false;
			if (this._media != null && this._media.TextureProducer != null)
			{
				Texture texture = this._media.TextureProducer.GetTexture(0);
				if (texture != null)
				{
					if (texture != this._lastTextureApplied)
					{
						this._isDirty = true;
					}
					if (this._isDirty)
					{
						int textureCount = this._media.TextureProducer.GetTextureCount();
						for (int i = 0; i < textureCount; i++)
						{
							texture = this._media.TextureProducer.GetTexture(i);
							if (texture != null)
							{
								this.ApplyMapping(texture, this._media.TextureProducer.RequiresVerticalFlip(), i);
							}
						}
					}
					flag = true;
				}
			}
			if (!flag)
			{
				if (this._defaultTexture != this._lastTextureApplied)
				{
					this._isDirty = true;
				}
				if (this._isDirty)
				{
					this.ApplyMapping(this._defaultTexture, false, 0);
				}
			}
		}

		// Token: 0x06000092 RID: 146 RVA: 0x000060B8 File Offset: 0x000042B8
		private void ApplyMapping(Texture texture, bool requiresYFlip, int plane = 0)
		{
			if (this._mesh != null)
			{
				this._isDirty = false;
				Material[] materials = this._mesh.materials;
				if (materials != null)
				{
					foreach (Material material in materials)
					{
						if (material != null)
						{
							if (plane == 0)
							{
								material.SetTexture(this._texturePropertyName, texture);
								this._lastTextureApplied = texture;
								if (texture != null)
								{
									if (requiresYFlip)
									{
										material.SetTextureScale(this._texturePropertyName, new Vector2(this._scale.x, -this._scale.y));
										material.SetTextureOffset(this._texturePropertyName, Vector2.up + this._offset);
									}
									else
									{
										material.SetTextureScale(this._texturePropertyName, this._scale);
										material.SetTextureOffset(this._texturePropertyName, this._offset);
									}
								}
							}
							else if (plane == 1 && material.HasProperty(ApplyToMesh._propUseYpCbCr) && material.HasProperty(ApplyToMesh._propChromaTex))
							{
								material.EnableKeyword("USE_YPCBCR");
								material.SetTexture(ApplyToMesh._propChromaTex, texture);
								if (requiresYFlip)
								{
									material.SetTextureScale("_ChromaTex", new Vector2(this._scale.x, -this._scale.y));
									material.SetTextureOffset("_ChromaTex", Vector2.up + this._offset);
								}
								else
								{
									material.SetTextureScale("_ChromaTex", this._scale);
									material.SetTextureOffset("_ChromaTex", this._offset);
								}
							}
							if (this._media != null)
							{
								if (material.HasProperty(ApplyToMesh._propStereo))
								{
									Helper.SetupStereoMaterial(material, this._media.m_StereoPacking, this._media.m_DisplayDebugStereoColorTint);
								}
								if (material.HasProperty(ApplyToMesh._propAlphaPack))
								{
									Helper.SetupAlphaPackedMaterial(material, this._media.m_AlphaPacking);
								}
								if (material.HasProperty(ApplyToMesh._propApplyGamma) && this._media.Info != null)
								{
									Helper.SetupGammaMaterial(material, this._media.Info.PlayerSupportsLinearColorSpace());
								}
							}
						}
					}
				}
			}
		}

		// Token: 0x06000093 RID: 147 RVA: 0x000062F4 File Offset: 0x000044F4
		private void OnEnable()
		{
			if (this._mesh == null)
			{
				this._mesh = base.GetComponent<MeshRenderer>();
				if (this._mesh == null)
				{
					GlobalData.Debug_LogWarning("[AVProVideo] No mesh renderer set or found in gameobject");
				}
			}
			this._isDirty = true;
			if (this._mesh != null)
			{
				this.LateUpdate();
			}
		}

		// Token: 0x06000094 RID: 148 RVA: 0x00006358 File Offset: 0x00004558
		private void OnDisable()
		{
			this.ApplyMapping(this._defaultTexture, false, 0);
		}

		// Token: 0x0400007D RID: 125
		[SerializeField]
		[Header("Media Source")]
		private MediaPlayer _media;

		// Token: 0x0400007E RID: 126
		[Tooltip("Default texture to display when the video texture is preparing")]
		[SerializeField]
		private Texture2D _defaultTexture;

		// Token: 0x0400007F RID: 127
		[SerializeField]
		[Space(8f)]
		[Header("Renderer Target")]
		private Renderer _mesh;

		// Token: 0x04000080 RID: 128
		[SerializeField]
		private string _texturePropertyName = "_MainTex";

		// Token: 0x04000081 RID: 129
		[SerializeField]
		private Vector2 _offset = Vector2.zero;

		// Token: 0x04000082 RID: 130
		[SerializeField]
		private Vector2 _scale = Vector2.one;

		// Token: 0x04000083 RID: 131
		private bool _isDirty;

		// Token: 0x04000084 RID: 132
		private Texture _lastTextureApplied;

		// Token: 0x04000085 RID: 133
		private static int _propStereo;

		// Token: 0x04000086 RID: 134
		private static int _propAlphaPack;

		// Token: 0x04000087 RID: 135
		private static int _propApplyGamma;

		// Token: 0x04000088 RID: 136
		private const string PropChromaTexName = "_ChromaTex";

		// Token: 0x04000089 RID: 137
		private static int _propChromaTex;

		// Token: 0x0400008A RID: 138
		private const string PropUseYpCbCrName = "_UseYpCbCr";

		// Token: 0x0400008B RID: 139
		private static int _propUseYpCbCr;
	}
}
